using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Web;
using Toast.Core;
using Toast.Core.Interfaces;
using Toast.Extensions;

namespace Toast.ContentTypes
{
    public abstract class JavaScriptContentBase : IJavaScriptContent
    {
        readonly string _id = string.Empty;

        ICompressionStrategy _javaScriptCompression = Core.ToastCore.Compression().ForJavaScript();

        #region IContent Members

        public string Id
        {
            get { return Regex.Replace(FileName, @"[\\\/\.\?\&\s]", "-"); }
        }

        public string FileName { get; set; }

        public virtual InjectAt InjectAt { get; set; }

        public abstract IInjectionFormatter GetFormatter(string packageKey);

        public string HttpContentType
        {
            get { return "text/javascript"; }
        }

        public virtual Func<bool> InjectIf { get; set; }

        public virtual ICompressionStrategy Compression
        {
            get { return _javaScriptCompression; }
            set { _javaScriptCompression = value; }
        }

        public abstract bool TryConstructFrom(string from, out IContent result);

        #endregion
        
        protected string GenerateFileName()
        {
            return ShortGuid.NewGuid();
        }
    }
}